<template>
  <div v-if="readonly==1">{{format}}</div>
  <el-select v-model="name" placeholder="请选择" v-else filterable clearable>
    <el-option v-for="item in options" :key="item.id" :label="`${item.cargoSn} - ${item.cargoName}`" :value="item[valField]" />
  </el-select>
</template>

<script setup lang="ts">
import useCounterStore from "@/store/counter";
const counter = useCounterStore();
import { computed, onMounted } from "vue";
const { proxy } = getCurrentInstance();
let props = defineProps(["modelValue", "readonly", "val"]);
const emit = defineEmits(["update:modelValue", "fatherMethods"]);
const name = computed({
  get() {
    return props.modelValue ? props.modelValue : null;
  },
  set(value: any) {
    emit("update:modelValue", value);
    setFather(value);
  },
});
let valField = computed(() => {
  return props.val ? props.val : "id";
});
let options = ref([]);
let format = computed(() => {
  let index = proxy.$utils.findIndexByField(
    options.value,
    valField.value,
    props.modelValue
  );
  if (index != -1) return options.value[index].cargoName;
});
onMounted(() => {
  getInfo();
});
let getInfo = async () => {
  let list = await setCargos();
  options.value = [...list];
  console.log("计划rows", options.value);
};
let setFather = (value) => {
  let index = proxy.$utils.findIndexByField(
    options.value,
    valField.value,
    value
  );
  if (index != -1) emit("fatherMethods", options.value[index]);
};
let setCargos = async () => {
  let { data } = await proxy.$apis.queryStoreList();
  let list = [];
  for (let index = 0; index < data.length; index++) {
    const element = data[index];
    let current = proxy.$utils.findIndexByField(
      list,
      "cargoId",
      element.cargoId
    );
    //没有该货物
    if (current == -1) {
      element.supplier = [element.supplierId];
      list.push(element);
    } else {
      list[current].num = list[current].num + element.num;
      let supplier = [];
      if (list[current].hasOwnProperty("supplier")) {
        supplier = list[current].supplier;
      }
      supplier.push(element.supplierId);
      list[current].supplier = proxy.$utils.unique(supplier);
    }
  }
  return list;
};
</script>

<style lang="scss" scoped>
</style>
